﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;

using OfficeOpenXml;

namespace chad.performancecounters.library
{
    public class WriterHelper
    {
        public static Boolean CanCreateFile(String fileNameAndPath)
        {
            try
            {
                using(StreamWriter writer = new StreamWriter(fileNameAndPath))
                {
                    writer.WriteLine("Can Write");
                }

                FileInfo file = new FileInfo(fileNameAndPath);
                file.Delete();

                return true;
            }
            catch (Exception ex) { }

            return false;
        }

        public static void WriteCsvToFileSystem(String fileNameAndPath, PerformanceCounterItem[] counters)
        {
            using(StreamWriter writer = new StreamWriter(fileNameAndPath))
            {
                //writer.WriteLine(String.Format("{0},{1},{2},{3},{4}","ObjectName","CounterName","InstanceName","CollectedValue","CounterHelp"));
                writer.WriteLine(String.Format("{0},{1},{2},{3},{4}", "ObjectName", "CounterName", "InstanceName","CounterType", "CounterHelp"));

                foreach(var counter in counters.OrderBy(a=> a.ObjectName).ThenBy(a => a.CounterName).ThenBy(a => a.InstanceName))
                {
                    //writer.WriteLine(String.Format("{0},{1},{2},{3},{4}",counter.ObjectName,counter.CounterName,counter.InstanceName,counter.CollectedValue,counter.CounterHelp));
                    writer.WriteLine(String.Format("{0},{1},{2},{3},{4}", counter.ObjectName, counter.CounterName, counter.InstanceName, counter.CounterType, counter.CounterHelp));
                }
            }

        }

        public static void WriteExcelToFileSystem(String fileNameAndPath, PerformanceCounterItem[] counters, String worksheetName = "PerformanceCounters")
        {
            ExcelPackage pkg = new ExcelPackage();

            pkg.Workbook.Worksheets.Add(worksheetName);

            ExcelWorksheet sheet = pkg.Workbook.Worksheets[1];

            //Headers
            sheet.Cells[1, 1].Value = "ObjectName";
            sheet.Cells[1, 2].Value = "CounterName";
            sheet.Cells[1, 3].Value = "InstanceName";
            sheet.Cells[1, 4].Value = "CounterType";
            sheet.Cells[1, 5].Value = "CounterHelp";

            //Formatting
            sheet.Column(1).Width = 30;
            sheet.Column(2).Width = 30;
            sheet.Column(3).Width = 30;
            sheet.Column(4).Width = 18;
            sheet.Column(5).Width = 100;
            sheet.Column(5).Style.WrapText = true;

            //Values
            int i = 2;
            foreach (var c in counters.OrderBy(a => a.ObjectName).ThenBy(a => a.CounterName).ThenBy(a => a.InstanceName))
            {
                sheet.Cells[i, 1].Value = c.ObjectName;
                sheet.Cells[i, 2].Value = c.CounterName;
                sheet.Cells[i, 3].Value = c.InstanceName;
                sheet.Cells[i, 4].Value = c.CounterType;
                sheet.Cells[i, 5].Value = c.CounterHelp;

                sheet.Row(i).Height = 15;

                i++;
            }

            //Table
            sheet.Tables.Add(new ExcelAddressBase(1, 1, i-1, 5), "Counters");
            sheet.Tables[0].TableStyle = OfficeOpenXml.Table.TableStyles.Light1;

            //Save
            pkg.SaveAs(new FileInfo(fileNameAndPath));
        }

    }
}
